package com.ysd.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ysd.entity.News;
import com.ysd.entity.User;
import com.ysd.util.DBManager;
import com.ysd.util.Dateutil;

public class UserDaoImpl {
	//增删改业务,需要返回受影响行数
	public int addMessage(User user){
	return DBManager.executeUpdate("insert into user(username,phone,email,content) values('"+user.getUsername()+"','"+user.getPhone()+"','"+user.getEmail()+"','"+user.getContent()+"')");	
		
	}
	
	public List<User> getUserStatus(String status,int page,int size){
		
		List<User> userList = new ArrayList<User>();
		
		Connection conn = DBManager.getConnection();
		Statement st = null;
		ResultSet rs = null;
		
		try {
			st = conn.createStatement();
			String sql = "select * from user where 1=1";
			if(status!=null && !status.equals("")) {
				sql+=" and status = '"+status+"'";
			}
			sql +=" order by time desc";
			 if(page>0) {
				 sql +=" limit "+(page-1)*size+","+size;
			 }
	
			rs = st.executeQuery(sql);
			while(rs.next()) {
				int userId=rs.getInt("userId");
				String username=rs.getString("username");
				String phone=rs.getString("phone");
				String email=rs.getString("email");
				String content=rs.getString("content");
				String time = rs.getString("time");
				String status1 = rs.getString("status");
				String reply = rs.getString("reply");
				
				User user = new User(userId, username, phone, email, content, time, status1, reply);
				
				userList.add(user);
			}
			return userList;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}finally {
			DBManager.closeAll(conn, st, rs);
		}
		
		
	}

	
public int getUser(String status){
		
	int count = 0;
		
		Connection conn = DBManager.getConnection();
		Statement st = null;
		ResultSet rs = null;
		
		try {
			st = conn.createStatement();
			String sql = "select count(*) from user where 1=1";
			if(status!=null && !status.equals("")) {
				sql+=" and status = '"+status+"'";
			}
			
			 
			rs = st.executeQuery(sql);
			while(rs.next()) {
				 count =rs.getInt("count(*)");
			}
			return count;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return -1;
		}finally {
			DBManager.closeAll(conn, st, rs);
		}
		
		
	}

	
public User getUserById(int uId){
		
		User user = null;
		
		Connection conn = DBManager.getConnection();
		Statement st = null;
		ResultSet rs = null;
		
		try {
			st = conn.createStatement();
			String sql = "select * from user where userId="+uId;
			rs = st.executeQuery(sql);
			while(rs.next()) {
				int userId=rs.getInt("userId");
				String username=rs.getString("username");
				String phone=rs.getString("phone");
				String email=rs.getString("email");
				String content=rs.getString("content");
				String time = rs.getString("time");
				String status1 = rs.getString("status");
				String reply = rs.getString("reply");
				
				 user = new User(userId, username, phone, email, content, time, status1, reply);
		
			}
			return user;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}finally {
			DBManager.closeAll(conn, st, rs);
		}
		
		
	}
/**
 * 修改留言状态
 * @param user
 * @return
 */
public int updateuserById(User user) {
	 
	return DBManager.executeUpdate("update user set status='"+user.getStatus()+"'where userId="+user.getUserId());
	 
}

public int updateuserReplyById(User user) {
	 
	return DBManager.executeUpdate("update user set status='已回复',reply='"+user.getReply()+"' where userId="+user.getUserId());
	 
}

	/**
	 * 根据id删除留言
	 * @param userId分类id
	 * @return
	 */
	
public int deluserId(int userId) {
	 
	return DBManager.executeUpdate("DELETE from user WHERE userId  ="+userId);
	 
}

public int deleteMessageByIds(String ids) {
	return DBManager.executeUpdate("DELETE from user where userId in("+ids+")");
}
	
}
